<?php 

	require_once($GLOBALS['PAD'].'Modules/MF93FL27/lib/func.mex');
 
	if ($_POST['bns_Type'] && $_POST['bns_Amount'] && (isset($_POST['CliNumero']) || isset($_POST['bac_Client']))) {
	$fi7 = $_POST['bns_Type'];
	$fi = $GLOBALS['MYT']->db_array("SELECT INVOICE FROM bonusinvoice WHERE bns_Type = {$fi7} ORDER BY REF DESC LIMIT 0,1");
	if (!isset($fi[1]['INVOICE'])) { $fi[1]['INVOICE'] = 0; }
	$fi1 = $fi[1]['INVOICE']+1;
	$fi2 = $_POST['CliNumero'];
	$fi3 = 0;
	$fi4 = $sysTime;
	$fi5 = $_SESSION[$SESSID];
	$fi6 = $_POST['bns_Amount'];
	$fi = $GLOBALS['MYT']->db_array("SELECT bns_CliReference FROM bonusinvoice WHERE CLIENT = {$fi2} ORDER BY REF DESC LIMIT 0,1");
	if (!isset($fi[1]['bns_CliReference'])) { $fi[1]['bns_CliReference'] = 1; }
	$fi8 = $fi[1]['bns_CliReference'];
	$fi9 = $_SESSION[$SESSCD];
	$fi10 = 1;
	$fi11 = $_POST['bns_Value'];
	$fi12 = $_POST['bns_ETime'];
	if(isset($_POST['bns_Partner'])) { $_POST['bns_Product'] = 0; $_POST['bns_Quant'] = 0;}
	if (!isset($_POST['bns_Product'])) {$_POST['bns_Product'] = 32;$fiQuant=1;}
	$fi13 = $_POST['bns_Product'];
	$fiQuant = $_POST['bns_Quant'];
	$fi14 = 0;
	$fi15 = 0;
	$fi16 = 0;
	if (isset($_POST['bns_TMoney'])) {
	$fi15 = $_POST['bns_Money'];
	$fi16 = $_POST['bns_TMoney'];	
	}
	
	if ($_POST['bns_Type'] == 4) {
		$ID = '10';
	} elseif ($_POST['bns_Type'] == 5) {
		$ID = '10';
	} elseif ($_POST['bns_Type'] == 6) {
		if ($fi16<1) {
		$ID = '09';
		} else {
		$ID = '14';		
		}
	} elseif ($_POST['bns_Type'] == 12) {
		$ID = '13';
		$fi14 = $_POST['bns_Partner'];
	} 
	$MovBalance = make_bnsbalance(0,$fi6,$fi2);
	if($GLOBALS['MYT']->db_query("INSERT INTO bonusinvoice 
		(INVOICE, CLIENT, CAMPAIGN, bns_Time, bns_User, Bns_Amount, bns_Type, bns_CliReference, bns_Origen1, bns_Currency, bns_Value, bns_ETime,bns_Balance,bns_Product,bns_Partner) 
		VALUES 
		({$fi1},{$fi2},{$fi3},'{$fi4}',{$fi5},-{$fi6},{$fi7},{$fi8},{$fi9},{$fi10},{$fi11},'{$fi12}',{$MovBalance},{$fi13},{$fi14})")==true) {
	$Obj = $GLOBALS['MYT']->db_array("SELECT REF FROM bonusinvoice WHERE bns_Type = {$fi7} AND INVOICE = {$fi1} AND bns_Time = '{$fi4}' AND bns_User = {$fi5}");
		
		if ($fi16<1) {
		$GLOBALS['MYT']->db_query("UPDATE bonus_productstocks SET bss_CrrQuant = bss_CrrQuant-{$fiQuant} 
			WHERE bss_Office = {$_SESSION[$SESSCD]} AND bss_Product = {$_POST['bns_Product']} AND bss_Status = 3 AND bss_CrrQuant > 0 AND bss_IniQuant != -1 LIMIT 1");
		} else {
			$GLOBALS['MYT']->db_query("INSERT INTO bonusencomendas (becClient, becUser, becOffice, becFlag, becProduct, becPartner, becDateRequest, becMoney, becUnitMoney, becQuant,becInvoice) VALUES 
					({$fi2},{$fi5},{$fi9},2,{$fi13},1,'{$fi4}',{$fi16},{$fi15},{$fiQuant},{$Obj[1]['REF']})");	
		}
		if($fi7 == 12 && $_POST['bacDoador'] == 0) {
			$GLOBALS['MYT']->db_query("UPDATE bonusaccounts SET bacDoador = 1 WHERE bac_Client = {$fi2}");
		}
		$Obj = $Obj[1]['REF'];
			if(!isset($_POST['NOOUTPUT'])) {
			echo '<div id="Printer"></div>';
			echo '<script charset="utf-8" type="text/javascript">newprint(\''.$GLOBALS['URL'].'Aplications/Export_Print.mex?OB='.$Obj.'&MOD=MF93FL27&ID='.$ID.'\',\'Printer\');</script>';
			echo '<div class="Confirm">';
			mn_field('button','ED','ben_print','Imprimir','','','','" onClick="newprint(\''.$GLOBALS['URL'].'Aplications/Export_Print.mex?OB='.$Obj.'&MOD=MF93FL27&ID='.$ID.'\',\'Printer\');');
			echo 'Emitido</div>';
			} else {
			
			echo 'Concluido <script charset="utf-8" type="text/javascript">Ext.getCmp(\'win_dtkben\').close();</script>';
			}
		}
	}